package Day13.ZuoYe;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class BookHandler {
    public static void main(String[] args) {
        SAXReader saxReader = new SAXReader();
        try {
            Document doc = saxReader.read(new FileInputStream("C:\\Users\\admin\\IdeaProjects\\java78\\javabasic\\src\\TeXing\\src\\Day13\\ZuoYe\\Bookstore.xml"));
            Element root = doc.getRootElement();
            List<Element> bookList=root.elements("book");
            List<Book> list=new ArrayList<>();
            Book book1 =null;
            for(Element book:bookList){
                String type = book.attributeValue("type");
                String title = book.elementTextTrim("title");
                String author = book.elementTextTrim("author");
                String year = book.elementTextTrim("year");
                String price = book.elementTextTrim("price");
                book1 = new Book(type, title, author, year, Double.parseDouble(price));
                System.out.println(book1.toString());
                list.add(book1);
            }
            System.out.println(">>>>>>>>>>>>>>>>>>>>")  ;
            for(Book o:list){
                System.out.println(o);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
